package com.geor.gcv.launcher;

import java.io.File;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;

import com.geor.common.msg.Strings;
import com.geor.common.system.Loader;
import com.geor.gcv.core.loader.EnvLoader;
import com.geor.gcv.core.mat.Mats;
import com.geor.gcv.core.test.TestSwitch;
import com.geor.gcv.core.test.Tester;
import com.geor.gcv.ict.util.PropertiesReader;

/**
 * <p>
 * 运行python tensorflow识别服务
 * <p>
 * 需要安装python3.5+ tensorflow
 * <p>
 * 建议使用Anaconda3部署环境
 * <p>
 * 默认加载config/command.properties配置
 * 
 * @author geor.lusongnan
 *
 */
// @Deprecated
public class TfLauncher extends Loader {
	/** python指令 */
	private String command;
	/** 源文件路径 */
	private String pypath;
	/** 模型文件 */
	private String trainDatapath;
	/** ip */
	private String adress;
	/** 分类的数量 */
	private String kinds = "4384";
	/** 端口 */
	private int port = 9998;

	/**
	 * 
	 * @param pypath
	 *            pyhon文件路径
	 * @param trainDatapath
	 *            checkpoint 训练路径
	 * @param adress
	 *            路径
	 * @param port
	 * 
	 */
	public TfLauncher(String command, String pypath, String trainDatapath) {
		this();
		this.command = command;
		this.pypath = pypath;
		this.trainDatapath = trainDatapath;
	}

	public TfLauncher() {
		super();
		this.adress = PropertiesReader.get("server.listener.address");
		this.port = Integer.parseInt(PropertiesReader.get("server.ccs.port"));
		this.command = PropertiesReader.get("command.command");
		this.pypath = PropertiesReader.get("command.pypath");
		this.trainDatapath = PropertiesReader.get("command.datapath");
		this.kinds = PropertiesReader.get("command.kinds");
	}

	public TfLauncher(String command, String pypath, String trainDatapath, String adress, int port) {
		super();
		this.command = command;
		this.pypath = pypath;
		this.trainDatapath = trainDatapath;
		this.adress = adress;
		this.port = port;
	}

	@Override
	public String getName() {
		return "chinese";
	}

	@Override
	public void load() throws Exception {
		if (!new File(pypath).exists()) {
			throw new RuntimeException(pypath + " is error");
		}

		if (!new File(pypath).exists()) {
			throw new RuntimeException(trainDatapath + " is error");
		}
		String exe = Strings.format("%s %s --ckpt_dir=%s --charset_size=%s --adress=%s --port=%d", command, pypath,
				trainDatapath, kinds, adress, port);
		log.debug(exe);
		Runtime.getRuntime().exec(exe);
	}

	@Override
	public void unload() throws Exception {
		// 发送消息告诉 python关闭？
	}

	@Override
	public String toString() {
		return "ChineseDataLoader [command=" + command + ", pypath=" + pypath + ", trainDatapath=" + trainDatapath
				+ ", adress=" + adress + ", port=" + port + "]";
	}

	public static void main(String[] args) throws Exception {
		new EnvLoader().load();

		TestSwitch.openImshow();

		Tester tester = new Tester();

		Mat mat = Mats.imread("C:/Users/Administrator/Desktop/pz/1.png");

		Core.line(mat, new Point(876, 1151), new Point(935, 696), new Scalar(0, 0, 255), 3);
		Core.line(mat, new Point(876, 1151), new Point(1592, 1243), new Scalar(0, 0, 255), 3);
		Core.line(mat, new Point(1592, 1243), new Point(1651, 788), new Scalar(0, 0, 255), 3);
		Core.line(mat, new Point(1651, 788), new Point(935, 696), new Scalar(0, 0, 255), 3);
		// 935,696; 876,1151 ;1592,1243 ;1651,788
		tester.showNewWindow("123", mat);

	}

}
